package com.googlecode.spruce.web.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.googlecode.spruce.dao.HostDao;
import com.googlecode.spruce.domain.Host;

@Controller
@RequestMapping("/v1/host")
public class HostController
{

    @Autowired
    private HostDao hostDao;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody
    Host getById(@PathVariable Integer id)
    {
        return hostDao.findByID(id);
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public @ResponseBody
    Integer create(@RequestBody final Host host)
    {
        Assert.notNull(host);
        Assert.hasText(host.getName(), "Host must have a name");
        Assert.isTrue(host.getOsID() > 0, "Host must have an OS ID");
        
        return hostDao.save(host);
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public @ResponseBody
    List<Host> getAll()
    {
        return hostDao.getAll();
    }

}
